package com.atsansan.mywork.login.pachong;

import com.atsansan.mywork.login.entity.BookEntity;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author sansan
 * @create 2021-04-07 18:09
 */
@Component
public class Info {


//    private static String url="https://www.amazon.cn/s?i=stripbooks&rh=n%3A1459679071&fs=true&page=2";
//    private static String url1="https://www.amazon.cn/-/en/s?i=stripbooks&bbn=1459678071&rh=n%3A658390051%2Cn%3A2045366051%2Cn%3A1459631071%2Cn%3A1459677071%2Cn%3A1459678071%2Cn%3A1459679071&dc&fs=true&page=2";
//    public static void main(String[] args) throws IOException {
//
//        List<String> listLink = getLink(url1);
//
//        List<String> bImgs = getImg(url1);
//        List<String> listName = getName(url1);
//        List<String> listAuthor = getAuthor(url1);
//        List<String> listPrice = getPrice(url1);
//        List<String> listLevel = getLevel(url1);
//        System.out.println(listLevel.toString());
//    }
//,List<String> bImgs,List<String> listName,List<String> listAuthor
//    ,List<String> listLink,List<String> listPrice,List<String> listLevel
    public static List<BookEntity> getBook(String url1) throws IOException {

        List<BookEntity> bookEntities=new ArrayList<>();

//        Long count= Long.valueOf(1);
        List<String> bImgs = getImg(url1);
        List<String> listName = getName(url1);
        List<String> listLink = getLink(url1);
        List<String> listAuthor = getAuthor(url1);
        List<String> listPrice = getPrice(url1);
        List<String> listLevel = getLevel(url1);
        for (int i = 0; i <listAuthor.size() ; i++) {
//            需要创建listAuthor.size()个对象,对每个对象进行赋值
            BookEntity bookEntitie= new BookEntity();
//            bookEntitie.setBId((long) (i+Math.random()*10));
            bookEntitie.setBImg(bImgs.get(i));
            bookEntitie.setBName(listName.get(i));
            bookEntitie.setBLink("https://www.amazon.cn"+listLink.get(i));
            bookEntitie.setBAuthor(listAuthor.get(i));
            bookEntitie.setBPrice(listPrice.get(i));
            bookEntitie.setBLevel(listLevel.get(0));
            bookEntities.add(bookEntitie);
//            count++;
        }
        return bookEntities;
    }
    public static List<String> getLink(String s) throws IOException {
        Document documentLink = null;
        Elements elementsLink=null;
        List<String> listLink =new ArrayList<>();
        String blink=null;
        Document document = Jsoup.connect(s).get();

        Elements elements = document.select("a.a-text-bold");
        documentLink=Jsoup.parse(String.valueOf(elements));
        elementsLink=documentLink.getElementsByClass("a-text-bold");

//        System.out.println(documentLink);
//        System.out.println(elementsLink);
        for (Element elements1:elementsLink){
            blink=elements1.attr("href");
            listLink.add(blink);
        }
//        System.out.println(listLink.size());
//        System.out.println(listLink.toString());
        return listLink;
    }

    public static List<String> getName(String s) throws IOException {
        Document documentName = null;
        Elements elementsName=null;
        List<String> listName =new ArrayList<>();
        String bName=null;
        Document document = Jsoup.connect(s).get();
//        a class="a-link-normal a-text-normal"
//        span class="a-size-medium a-color-base a-text-normal"
        Elements elements = document.select("a.a-text-normal");
        documentName=Jsoup.parse(String.valueOf(elements));
        elementsName=documentName.select("span.a-size-medium");

//        System.out.println(documentName);
//        System.out.println(elementsName);
        for (Element elements1:elementsName){
            bName=elements1.text();
            listName.add(bName);

        }
//        System.out.println(listName.size());
//        System.out.println(listName.toString());
        return listName;
    }
    public static List<String> getImg(String s) throws IOException {
        Document documentImg = null;
        Elements elementsImg = null;
        List<String> bImgs =new ArrayList<>();
        String bImg=null;
        Document document = Jsoup.connect(s).get();

        Elements elements = document.select("img.s-image");
        documentImg = Jsoup.parse(String.valueOf(elements));
        elementsImg = documentImg.getElementsByClass("s-image");

//        System.out.println(documentImg);
//        System.out.println(elementsImg);
        for (Element elements1 : elementsImg) {
            bImg = elements1.attr("src");
            bImgs.add(bImg);
//            System.out.println(bImg);

        }
//        System.out.println(bImgs.size());
//        System.out.println(bImgs.toString());
        return bImgs;


    }
    public static List<String> getAuthor(String s) throws IOException {
        Document documentAuthor = null;
        Elements elementsAuthor= null;
        Elements elementsAuthor1= null;
        Elements elementsAuthor2= null;
        String bAuthors = null;
        List<String> listAuthor =new ArrayList<>();
        Document document = Jsoup.connect(s).get();

//        div class="a-row a-size-base a-color-secondary"
//        span class="a-size-base"
        Elements elements = document.select("div.a-color-secondary");
        documentAuthor = Jsoup.parse(String.valueOf(elements));
        elementsAuthor = documentAuthor.select("span.a-color-secondary").remove();

        elementsAuthor2=documentAuthor.select("div.a-color-secondary");

//        String author = elementsAuthor1.text();

//        System.out.println(documentAuthor);
//        System.out.println(elementsAuthor);
//        System.out.println(elementsAuthor1);
//        System.out.println(author);
//        System.out.println(elementsAuthor2);
        elementsAuthor1=elementsAuthor2.select("span.a-size-base");
        bAuthors = elementsAuthor1.text();
        String[] bAuthor = bAuthors.split("  ");
        listAuthor=Arrays.asList(bAuthor);
//        System.out.println(listAuthor.toString());
        return listAuthor;
//        System.out.println(listAuthor.toString());
//        for (Element elements1 : elementsAuthor2) {
//            elementsAuthor1=elements1.select("span.a-size-base");
//            bAuthor = elementsAuthor1.text();
//            List<String> list=Arrays.asList(bAuthor);
//
//
//
//
////            System.out.println(Arrays.asList(bAuthor).toString());
////            System.out.println("##############");
//
//        }


    }
    public static List<String> getPrice(String s) throws IOException {
        Document documentPrice = null;
        Elements elementsPrice=null;
        List<String> listPrice =new ArrayList<>();
        String bPrice=null;
        Document document = Jsoup.connect(s).get();
//        span class="a-offscreen"
        Elements elements = document.select("span.a-offscreen");
        documentPrice=Jsoup.parse(String.valueOf(elements));
        elementsPrice=documentPrice.select("span.a-offscreen");

//        System.out.println(documentName);
//        System.out.println(elementsName);
        for (Element elements1:elementsPrice){
            bPrice=elements1.text();
            listPrice.add(bPrice);

        }
//        System.out.println(listPrice.size());
//        System.out.println(listPrice.toString());
        return listPrice;
    }




    public static List<String> getLevel(String s) throws IOException {
        Document documentLevel = null;
        Elements elementsLevel=null;
        List<String> listLevel =new ArrayList<>();
        String bLevel=null;
        Document document = Jsoup.connect(s).get();
//        span class="a-size-base a-color-base a-text-bold"
        Elements elements = document.select("span.a-list-item");
        documentLevel=Jsoup.parse(String.valueOf(elements));
        elementsLevel=documentLevel.select("span.a-text-bold");

//        System.out.println(documentName);
//        System.out.println(elementsName);
        for (Element elements1:elementsLevel){
            bLevel=elements1.text();
            listLevel.add(bLevel);

        }
//        System.out.println(listLevel.size());
//        System.out.println(listLevel.toString());
        return listLevel;
    }
//    private static void getHtml() {
//        HttpClient client = HttpClients.createDefault();
//        HttpGet httpGet=new HttpGet(url1);
//        httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36 Edg/89.0.774.68");
//
//        HttpResponse response=null;
//        try {
//
//            response=client.execute(httpGet);
//            HttpEntity httpEntity=response.getEntity();
//            System.out.println(EntityUtils.toString(httpEntity));
//        }catch (Exception e){
//
//        }finally {
//
//        }
//
//    }
}
